package com.tianji.learning;


import com.tianji.learning.Constants.RedisConstants;
import com.tianji.learning.domain.po.PointsBoard;
import com.tianji.learning.service.IPointsBoardService;
import com.tianji.learning.utils.TableInfoContext;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Set;

@SpringBootTest

public class Dinami {

    @Resource
    private  IPointsBoardService boardService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;



    @Test
    public void test1() {
        TableInfoContext.setInfo("points_board_12");
        PointsBoard pointsBoard = new PointsBoard();
        pointsBoard.setPoints(100);
        pointsBoard.setUserId(2L);
        boardService.save(pointsBoard);
    }
    @Test
    public void test2() {
        TableInfoContext.setInfo("points_board_5");
        String key= RedisConstants.POINTS_KEY_PREFIX+"202302";
        Set<ZSetOperations.TypedTuple<String>> typedTuples = redisTemplate.opsForZSet().reverseRangeWithScores(key, 0, -1);
        ArrayList<PointsBoard> list = new ArrayList<>(typedTuples.size());
        for (ZSetOperations.TypedTuple<String> tuple : typedTuples) {
            System.out.println(tuple.getValue());
            System.out.println(tuple.getScore());
            PointsBoard pointsBoard = new PointsBoard();
            pointsBoard.setPoints(tuple.getScore().intValue());
            pointsBoard.setUserId(Long.valueOf(tuple.getValue()));
            list.add(pointsBoard);
        }
        boardService.saveBatch(list);
        System.out.println(list);
    }
}
